﻿package ${serviceImpl.packageName};

import ${entity.fullName};
import cn.demoncat.util.lang.entity.Result;
<#if entity.hasName || entity.hasTitle>
import cn.demoncat.util.lang.ParamEmptyUtil;
</#if>
import ${entity.basePackage}.Base${entity.baseType}Dao;
import ${entity.basePackage}.Base${entity.baseType}ServiceImpl;
import ${mapper.packageName}.${entity.name}${mapperSuffix};
import ${service.packageName}.${entity.name}Service;
import cn.demoncat.log.util.UserLogUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

/** 
 * ${entity.comment}接口实现类
 *
 * @author ${entity.author}
 *
 * @since ${entity.time}
 */
@Service
public class ${entity.name}ServiceImpl extends Base${entity.baseType}ServiceImpl<${entity.name}, ${entity.idAttr.type}> implements ${entity.name}Service{

	@Resource
	private ${entity.name}${mapperSuffix} ${entity.simpleName}${mapperSuffix};
	@Resource
    private UserLogUtil userLogUtil;
    	
	/**
	 * 初始化DAO
	 */
	@Override
	protected Base${entity.baseType}Dao<${entity.name}, ${entity.idAttr.type}> getDao() {
		return ${entity.simpleName}${mapperSuffix};
	}
    
    /**
     * 添加
     *
     * @param data
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result add(${entity.name} data) {
        // 参数校验
        <#if entity.hasName>
        ParamEmptyUtil.checkBlank(data.getName());
        <#elseif entity.hasTitle>
        ParamEmptyUtil.checkBlank(data.getTitle());
        </#if>
        // 保存
        data.setId(null);
        <#if entity.hasSort>
        data.setSort(System.currentTimeMillis());
        </#if>
        this.save(data);
        // 操作日志
        <#if entity.hasName>
        userLogUtil.add(data.getId(),data.getName());
        <#elseif entity.hasTitle>
        userLogUtil.add(data.getId(),data.getTitle());
        <#else>
        userLogUtil.add(data.getId(),"日志备注");
        </#if>
        return Result.success(data);
    }

    /**
     * 修改
     *
     * @param data
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result upd(${entity.name} data) {
        // 参数校验
        <#if entity.hasName>
        ParamEmptyUtil.checkBlank(data.getName());
        <#elseif entity.hasTitle>
        ParamEmptyUtil.checkBlank(data.getTitle());
        </#if>
        // 查询原数据
        ${entity.name} dbData = this.getById(data.getId());
        if (dbData == null) {
            return Result.ERROR_DATA_NULL;
        }
        // 保存
        <#if entity.hasSort>
        data.setSort(null);
        </#if>
        this.update(data);
        // 操作日志
        <#if entity.hasName>
        userLogUtil.add(dbData.getId(),dbData.getName());
        <#elseif entity.hasTitle>
        userLogUtil.add(dbData.getId(),dbData.getTitle());
        <#else>
        userLogUtil.add(dbData.getId(),"日志备注");
        </#if>
        return Result.success(data);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result del(${entity.idAttr.type} id) {
        <#if entity.baseType == "Tree">
        // 删除
        ${entity.name} dbData = this.deleteAndGetTree(id);
        if (dbData != null) {
            // 操作日志
            <#if entity.hasName>
            userLogUtil.add(dbData.getId(),dbData.getName());
            <#elseif entity.hasTitle>
            userLogUtil.add(dbData.getId(),dbData.getTitle());
            <#else>
            userLogUtil.add(dbData.getId(),"日志备注");
            </#if>
        }
        return Result.SUCCESS;
        <#else>
        // 查询源数据
        ${entity.name} dbData = this.getById(id);
        if (dbData != null) {
            // 删除
            if (this.delete(id) > 0) {
                // 操作日志
                <#if entity.hasName>
                userLogUtil.add(dbData.getId(),dbData.getName());
                <#elseif entity.hasTitle>
                userLogUtil.add(dbData.getId(),dbData.getTitle());
                <#else>
                userLogUtil.add(dbData.getId(),"日志备注");
                </#if>
            }
        }
        </#if>
        return Result.SUCCESS;
    }

    <#if entity.hasSort>
    /**
     * 排序
     *
     * @param id
     * @param sort
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result sort(${entity.idAttr.type} id, Long sort) {
        // 查询源数据
        ${entity.name} dbData = this.getById(id);
        if (dbData == null) {
            return Result.ERROR_DATA_NULL;
        }
        // 排序
        if (this.updateSort(id,sort) > 0) {
            // 操作日志
            <#if entity.hasName>
            userLogUtil.add(dbData.getId(),dbData.getName());
            <#elseif entity.hasTitle>
            userLogUtil.add(dbData.getId(),dbData.getTitle());
            <#else>
            userLogUtil.add(dbData.getId(),"日志备注");
            </#if>
        }
        return Result.SUCCESS;
    }
    </#if>
}